home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / PowerMac / SpriteWorld / Examples / Simple / Simple.c < prev    next >
Encoding:
Text File  |  1994-04-25  |  4.5 KB  |  240 lines  |  [TEXT/KAHL]

  1. ///--------------------------------------------------------------------------------------
  2. // Simple.c
  3. //
  4. // By: Tony Myles
  5. //
  6. // Copyright © 1993 Tony Myles, All rights reserved worldwide.
  7. ///--------------------------------------------------------------------------------------
  8.  
  9.  
  10. #ifndef __DESK__
  11. #include <Desk.h>
  12. #endif
  13.  
  14. #ifndef __EVENTS__
  15. #include <Events.h>
  16. #endif
  17.  
  18. #ifndef __OSEVENTS__
  19. #include <OSEvents.h>
  20. #endif
  21.  
  22. #ifndef __TOOLUTILS__
  23. #include <ToolUtils.h>
  24. #endif
  25.  
  26. #ifndef __SEGLOAD__
  27. #include <SegLoad.h>
  28. #endif
  29.  
  30. #ifndef __WINDOWS__
  31. #include <Windows.h>
  32. #endif
  33.  
  34. #ifndef __SPRITEWORLD__
  35. #include <SpriteWorld.h>
  36. #endif
  37.  
  38. #ifndef __SPRITELAYER__
  39. #include <SpriteLayer.h>
  40. #endif
  41.  
  42. #ifndef __SPRITE__
  43. #include <Sprite.h>
  44. #endif
  45.  
  46. #ifndef __FRAME__
  47. #include <Frame.h>
  48. #endif
  49.  
  50. #ifndef __SPRITEWORLDUTILS__
  51. #include <SpriteWorldUtils.h>
  52. #endif
  53.  
  54. #ifndef __APPLICATION__
  55. #include "Application.h"
  56. #endif
  57.  
  58. #ifndef __SIMPLE__
  59. #include "Simple.h"
  60. #endif
  61.  
  62.  
  63. ///--------------------------------------------------------------------------------------
  64. // PerformSimpleAnimation
  65. ///--------------------------------------------------------------------------------------
  66.  
  67. void PerformSimpleAnimation(CWindowPtr srcWindowP)
  68. {
  69.     OSErr err;
  70.     GrafPtr savePort;
  71.     PixPatHandle pixPatH;
  72.     SpriteWorldPtr spriteWorldP;
  73.     SpriteLayerPtr spriteLayerP;
  74.     SpritePtr simpleSpriteArray[kNumberOfSprites];
  75.     SpritePtr simpleSpriteP;
  76.     Rect moveBoundsRect;
  77.     short spriteNum;
  78.  
  79.     GetPort(&savePort);
  80.     SetPort((GrafPtr)srcWindowP);
  81.     SetCursor(*GetCursor(watchCursor));
  82.  
  83.  
  84.     //
  85.     // STEP #1: initialize the sprite world package
  86.     //
  87.  
  88.     err = SWEnterSpriteWorld();
  89.     FatalError(err);
  90.  
  91.  
  92.     //
  93.     // STEP #2: create the various pieces that we need
  94.     //
  95.  
  96.         // create the sprite world
  97.     err = SWCreateSpriteWorldFromWindow(&spriteWorldP, srcWindowP, NULL);
  98.     FatalError(err);
  99.  
  100.         // create the sprite layer
  101.     err = SWCreateSpriteLayer(&spriteLayerP);
  102.     FatalError(err);
  103.  
  104.         // create the first sprite
  105.     err = SWCreateSpriteFromCIconResource(&simpleSpriteP, NULL, kSpriteCIconResID,
  106.             kNumberOfSpriteFrames, kRegionMask);
  107.     FatalError(err);
  108.     simpleSpriteArray[0] = simpleSpriteP;
  109.  
  110.         // clone the rest of the sprites off the first one
  111.     for (spriteNum = 1; spriteNum < kNumberOfSprites; spriteNum++)
  112.     {
  113.         err = SWCloneSprite(simpleSpriteP, simpleSpriteArray + spriteNum, NULL);
  114.         FatalError(err);
  115.     }
  116.  
  117.  
  118.     //
  119.     // STEP #3: put the pieces together (must be done BEFORE the sprite world is locked!)
  120.     //
  121.  
  122.     for (spriteNum = 0; spriteNum < kNumberOfSprites; spriteNum++)
  123.     {
  124.             // add the sprite to the layer
  125.         SWAddSprite(spriteLayerP, simpleSpriteArray[spriteNum]);
  126.     }
  127.  
  128.         // add the layer to the world
  129.     SWAddSpriteLayer(spriteWorldP, spriteLayerP);
  130.  
  131.  
  132.     //
  133.     // STEP #4: set things up for the animation
  134.     //
  135.  
  136.         // calculate the movement boundary rectangle
  137.     moveBoundsRect = spriteWorldP->windowFrameP->frameRect;
  138.  
  139.     for (spriteNum = 0; spriteNum < kNumberOfSprites; spriteNum++)
  140.     {
  141.         simpleSpriteP = simpleSpriteArray[spriteNum];
  142.  
  143.             // set up the sprite
  144.         SWSetSpriteMoveBounds(simpleSpriteP, &moveBoundsRect);
  145.         SWSetSpriteMoveTime(simpleSpriteP, kSpriteMoveTime);
  146.         SWSetSpriteMoveProc(simpleSpriteP, SWBounceSpriteMoveProc);
  147.         SWSetSpriteMoveDelta(simpleSpriteP, kHorizMoveDelta, kVertMoveDelta);
  148.  
  149.             // set the sprite’s initial location
  150.         SWSetSpriteLocation(simpleSpriteP, spriteNum * 60, spriteNum * 60);
  151.     }
  152.  
  153.  
  154.         // draw a nice background
  155.  
  156.     SetPort(spriteWorldP->backFrameP->framePort.monoGrafP);
  157.  
  158.     if (SWHasColorQuickDraw())
  159.     {
  160.         pixPatH = GetPixPat(kPixPatResID);
  161.  
  162.         if (pixPatH != NULL)
  163.         {
  164.             FillCRect(&moveBoundsRect, pixPatH);
  165.             DisposePixPat(pixPatH);
  166.         }
  167.         else
  168.         {
  169.             FillRect(&moveBoundsRect, qd.ltGray);
  170.         }
  171.     }
  172.     else
  173.     {
  174.         FillRect(&moveBoundsRect, qd.ltGray);
  175.     }
  176.  
  177.     SetPort((GrafPtr)srcWindowP);
  178.  
  179.  
  180.     //
  181.     // STEP #5: lock the sprite world        !!! VERY IMPORTANT !!!
  182.     //
  183.  
  184.     SWLockSpriteWorld(spriteWorldP);
  185.  
  186.  
  187.     //
  188.     // STEP #6: run the animation
  189.     //
  190.  
  191.         // update the window
  192.     SWUpdateSpriteWorld(spriteWorldP);
  193.     HideCursor();
  194.  
  195.     while (!Button())
  196.     {
  197.         SWProcessSpriteWorld(spriteWorldP);
  198.         SWAnimateSpriteWorld(spriteWorldP);
  199.  
  200.             // be friendly to drivers
  201.         SystemTask();
  202.     }
  203.  
  204.  
  205.     //
  206.     // STEP #7: unlock the sprite world
  207.     //
  208.  
  209.     SWUnlockSpriteWorld(spriteWorldP);
  210.  
  211.  
  212.     //
  213.     // STEP #8: dispose of the pieces we created
  214.     //
  215.  
  216.     for (spriteNum = 0; spriteNum < kNumberOfSprites; spriteNum++)
  217.     {
  218.         SWDisposeSprite(simpleSpriteArray[spriteNum]);
  219.     }
  220.  
  221.     SWDisposeSpriteLayer(spriteLayerP);
  222.     SWDisposeSpriteWorld(spriteWorldP);
  223.  
  224.  
  225.     //
  226.     // STEP #9: shut down the sprite world package
  227.     //
  228.  
  229.     SWExitSpriteWorld();
  230.  
  231.  
  232.     FlushEvents(everyEvent, 0);
  233.     SetPort(savePort);
  234.     ShowCursor();
  235.     SetCursor(&qd.arrow);
  236. }
  237.  
  238.  
  239.  
  240.